package com.swm.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swm.dao.ProjectDao;
import com.swm.dao.UserDao;
import com.swm.jdo.ProjectDaoImpl;
import com.swm.jdo.UserDaoImpl;
import com.swm.model.DataManager;
import com.swm.model.MyProject;
import com.swm.model.Project;
import com.swm.model.Testcase;
import com.swm.model.User;


@Service	//Spring will create a bean from this class for DI
public class ProjectService {
	@Autowired	//Spring will inject this dependency for us
    private ProjectDao projectDao = new ProjectDaoImpl();
	
	@Autowired	//Spring will inject this dependency for us
    private UserDao userDao = new UserDaoImpl();
	public DataManager getDataManager(){
		return DataManager.getInstance();
	}
	public boolean AddProject( Project project ){				
		if ( projectDao == null )
			return false;
			
		this.projectDao.Add(project);
		getDataManager().addProject(project);
		System.out.println( "[CREATE PROJECT][KEY] = " + project.getKey() );
		return true;		
	}
	
	public boolean AddProjectTestcase( Testcase testcase, String userID ){				
		
		User user 		= getDataManager().getUser( userID );
		Project project = getDataManager().getProject(testcase.getProjectName());
		
		if ( projectDao == null ) 	return false;
		if ( testcase == null )		return false;
		
		if ( user == null ){
			System.out.println("[Testcase] User Null");
			return false;	
		}
		if ( project == null ){
			System.out.println("[Testcase] Project Null");
			return false;		
		}
		if ( !project.isBeingUser( userID )){
			System.out.println("[Testcase] Invalid User");
			return false;
		}
		
		this.projectDao.Add( testcase );
		getDataManager().addTestcase( project, testcase );
		System.out.println( "[CREATE TESTCASE][KEY] = " + testcase.getKey() );
		return true;
	}
	
	/*
	public void UpdateProject( Project project){
		this.projectDao.Update(project);
	}
	*/
	public void DeleteUser( String projectID ){
		this.projectDao.Delete(projectID);
	}
	
	public List<Project> getAllProject(){
		return this.projectDao.find();
	}
	public Project	getProject( String projectID ){
		return this.projectDao.find(projectID);
	}
	
	public boolean addProjectUser( String projectID, String userID ){	
		
		Project project = getDataManager().getProject( projectID );
		if ( project != null )
		{
			User user = getDataManager().getUser( userID );
			if ( user != null ){
				project.addUser( user );
				return true;
			}
		}
		return false;
	}
	
	/*
	public List<MyProject> getProjectList(String userID) {			
		
		List<Project> projects = getDataManager().getProjects();	
		List<MyProject> myProject = new ArrayList<MyProject>();		
		if ( projects.size() > 0 )
		{
			for ( Project project : projects ){
				if ( project.isBeingUser(userID) ){			
					myProject.add( new MyProject( project.getName()) );
					System.out.println( "MyProjectList : " + project.getName() );
				}
			}
		}
		return myProject;
	}
	*/
	public String getProjectList(String userID) {			
		
		StringBuffer myProject = new StringBuffer();
		List<Project> projects = getDataManager().getProjects();	
		if ( projects.size() > 0 )
		{
			int i = 0;
			for ( Project project : projects ){
				if ( project.isBeingUser(userID) ){			
					myProject.append( project.getName() );
					if ( projects.size()-1 != i ){
						myProject.append(";");
					}
					System.out.println( "MyProjectList : " + project.getName() );
				}
				i++;
			}
		}
		//myProject.setCharAt( myProject.length()-1, ' ' );
		return myProject.toString();
	}
	
	/*
	public List<Testcase> getMyTestcaseList( String projectID ){
		Project project = getDataManager().getProject( projectID );
		if ( project == null )	
			return null;
		
		return project.getTestcaseList();				
	}
	*/
	public String getMyTestcaseList( String projectID ){
		Project project = getDataManager().getProject( projectID );
		if ( project == null )	
			return null;
		
		StringBuffer myTestcase = new StringBuffer();
		List<Testcase> myTestcaseList = project.getTestcaseList();
		int i = 0;
		for ( Testcase testcase : myTestcaseList ){
			myTestcase.append( testcase.getProjectName() + "+" );
			myTestcase.append( testcase.getTestcaseName() + "+" );
			myTestcase.append( testcase.getContents());
			
			if ( myTestcaseList.size()-1 != i ){
				myTestcase.append(";");
			}
			i++;
		}
		return myTestcase.toString();
	}
	
	public boolean deleteTestcase( String projectID , String testcaseID ){
		Project project = getDataManager().getProject( projectID );		
		if ( project == null )	
			return false;
		
		Testcase testcase = project.getTestcase( testcaseID );
		if ( testcase == null )
			return false;
		
		projectDao.DeleteTestcase( projectID , testcaseID );
		return getDataManager().deleteTestcase( project, testcase );		
	}
	
	public boolean modifyTestcase( Testcase testcase ) {
				
		Project project = getDataManager().getProject( testcase.getProjectName()  );		
		if ( project == null )	
			return false;
		
		// existing validation
		if ( project.getTestcase( testcase.getTestcaseName() ) == null )
			return false;
		
		projectDao.Update( testcase );
		return getDataManager().modifyTestcase( project, testcase );		
	}
	
}
